package com.huaxinkai.mobile.util.wechatUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.huaxinkai.mobile.util.wechatUtil.bean.Article;
import com.huaxinkai.mobile.util.wechatUtil.bean.MsgMediaId;
import com.huaxinkai.mobile.util.wechatUtil.bean.WeChatResNewsMessage;
import com.huaxinkai.mobile.util.wechatUtil.bean.WeChatResTextMessage;
import com.huaxinkai.mobile.util.wechatUtil.bean.WechatResImageMesage;
import com.huaxinkai.mobile.util.wechatUtil.bean.WechatResVoiceMesage;
import com.huaxinkai.mobile.util.wechatUtil.bean.WechatTextMenu;


/**
 * 微信公众号消息处理业务
 * @author liuzh
 * @data 2016年12月31日下午3:20:58
 */
public class WechatCoreService {
	/**
	 * 处理微信消息
	 * @param request
	 * @return
	 */
	public static String processRequest(HttpServletRequest request) {
		Map<String, String> map = null;
		try {
			map =  WeChatParserXmlUtils.parseXml(request);
		} catch (Exception e) {
		}
		String fromUserName = map.get("FromUserName");
		
		String toUserName = map.get("ToUserName");
		
		String msgType = map.get("MsgType");
		
		String content = map.get("Content");
		System.out.println("消息类型："+msgType);
		
		if(REQ_TEXT.equals(msgType)){
			String textMesssage = null;
			if("1".equals(content)){
				textMesssage = textMesssage(fromUserName,toUserName,msgType,WechatTextMenu.menuCtentOne());
			}else if("2".equals(content)){
				textMesssage = textMesssage(fromUserName,toUserName,msgType,WechatTextMenu.menuCtentTwo());
			}else if("?".equals(content) || "？".equals(content)){
				textMesssage = textMesssage(fromUserName,toUserName,msgType,WechatTextMenu.oneMenu());
			}else{
				textMesssage = newsMesssage(fromUserName,toUserName,msgType);
			}
			return textMesssage;
		}
		else if(REQ_IMAGE.equals(msgType)){
			String textMesssage = null;
			textMesssage = imageMesssage(fromUserName,toUserName,msgType,map.get("MediaId"));
			return textMesssage;
		}
		else if(REQ_VOICE.equals(msgType)){
			String textMesssage = null;
			textMesssage = voiceMesssage(fromUserName,toUserName,msgType,map.get("MediaId"));
			return textMesssage;
		}
		else if(REQ_NEWS.equals(msgType)){
			String textMesssage = null;
			textMesssage = newsMesssage(fromUserName,toUserName,msgType);
			return textMesssage;
		}
		else if(REQ_EVENT.equals(msgType)){
			String event = map.get("Event");
			String textMesssage = null;
			// 关注
			if(EVENT_SUBSCRIBE.equals(event)){
				textMesssage = textMesssage(fromUserName,toUserName,msgType,WechatTextMenu.menuSubscribe());
			}
			else if(EVENT_UNSUBSCRIBE.equals(event)){
				textMesssage = textMesssage(fromUserName,toUserName,msgType,WechatTextMenu.menuUnSubscribe());
			}
			else if(EVENT_SCAN.equals(event)){
				textMesssage = textMesssage(fromUserName,toUserName,msgType,WechatTextMenu.menuSubscribe());
			}
			return textMesssage;
		}
		return "";
	}
	
	/**
	 * 回复文本消息
	 * @param fromUserName
	 * @param toUserName
	 * @param msgType
	 * @param content
	 * @return
	 */
	public static String textMesssage(String fromUserName,String toUserName,String msgType,String content){
		WeChatResTextMessage bm = new WeChatResTextMessage();
		bm.setFromUserName(toUserName);
		bm.setToUserName(fromUserName);
		bm.setMsgType(msgType);
		bm.setCreateTime(new Date().getTime());
		bm.setContent(content);
		return WeChatParserXmlUtils.textMessageToXml(bm);
	}
	/**
	 *  回复图片消息
	 * @param fromUserName
	 * @param toUserName
	 * @param msgType
	 * @param mediaId
	 * @return
	 */
	public static String imageMesssage(String fromUserName,String toUserName,String msgType,String  mediaId){
		MsgMediaId msgMediaIdnew = new MsgMediaId();
		msgMediaIdnew.setMediaId(mediaId);
		WechatResImageMesage image = new WechatResImageMesage();
		image.setFromUserName(toUserName);
		image.setToUserName(fromUserName);
		image.setMsgType(msgType);
		image.setCreateTime(new Date().getTime());
		image.setImage(msgMediaIdnew);
		return WeChatParserXmlUtils.imageMessageToXml(image);
	}
	/**
	 *  回复语音消息
	 * @param fromUserName
	 * @param toUserName
	 * @param msgType
	 * @param mediaId
	 * @return
	 */
	public static String voiceMesssage(String fromUserName,String toUserName,String msgType,String  mediaId){
		MsgMediaId msgMediaIdnew = new MsgMediaId();
		msgMediaIdnew.setMediaId(mediaId);
		WechatResVoiceMesage voice = new WechatResVoiceMesage();
		voice.setFromUserName(toUserName);
		voice.setToUserName(fromUserName);
		voice.setMsgType(msgType);
		voice.setCreateTime(new Date().getTime());
		voice.setVoice(msgMediaIdnew);
		return WeChatParserXmlUtils.voiceMessageToXml(voice);
	}

	/**
	 * 回复图文消息
	 * @param fromUserName
	 * @param toUserName
	 * @param msgType
	 * @return
	 */
	public static String newsMesssage(String fromUserName,String toUserName,String msgType){
		Article article = new Article();
		article.setTitle("华新凯");
		article.setDescription("这是来自华新凯的图片，点击图片看更多...\n回复？获取菜单");
		article.setPicUrl("http://huaxinkai.imwork.net/resources/static/index/image/index.jpg");
		article.setUrl("http://huaxinkai.imwork.net");
		List<Article> list = new ArrayList<Article>();
		list.add(article);
		WeChatResNewsMessage news = new WeChatResNewsMessage();
		news.setFromUserName(toUserName);
		news.setToUserName(fromUserName);
		news.setArticles(list);
		news.setArticleCount(list.size());
		news.setMsgType("news");
		return WeChatParserXmlUtils.newsMessageToXml(news);
	}
	
	/** 请求消息类型：文本 */
	public static final String REQ_TEXT = "text";
	/** 请求消息类型：图片 */
	public static final String REQ_IMAGE = "image";
	/** 请求消息类型：链接 */
	public static final String REQ_LINK = "link";
	/** 请求消息类型：地理位置 */
	public static final String REQ_LOCATION = "location";
	/** 请求消息类型：语音 */
	public static final String REQ_VOICE = "voice";
	/** 请求消息类型：图文 */
	public static final String REQ_NEWS = "news";
	/** 请求消息类型：推送 */
	public static final String REQ_EVENT = "event";

	/** 事件类型：subscribe(订阅) */
	public static final String EVENT_SUBSCRIBE = "subscribe";
	/** 事件类型：unsubscribe(取消订阅) */
	public static final String EVENT_UNSUBSCRIBE = "unsubscribe";
	/** 事件类型：SCAN(已关注进入)  */
	public static final String EVENT_SCAN = "SCAN";
	/** 事件类型：CLICK(自定义菜单点击事件) */
	public static final String EVENT_CLICK = "CLICK";

	/** 返回消息类型：文本 */
	public static final String RESP_TEXT = "text";
	/** 返回消息类型：音乐 */
	public static final String RESP_MUSIC = "music";
	/** 返回消息类型：图文 */
	public static final String RESP_NEWS = "news";
}
